<?php
namespace app\admin\controller\v3;
use app\admin\Adminbase;
use app\common\model\Msg as MsgModel;
use app\common\model\MsgUser;
use app\common\model\MsgOrder;
use app\common\model\MsgSystem;

class Msg extends Adminbase 
{
	
	/**
	*发送系统消息
	* /admin/v3/msg/sendSystemMsg
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function sendSystemMsg(){
		$request =  request();
		$uid = $request->userId;
		$param = $request->post();
		
		//TODO 获取post参数
    	list($title,$content,$img,$buessType,$relationId,$id) = [
    		$param['title'] ?: '',
    		$param['content'] ?: '',
    		$param['img'] ?: '',
    		$param['buess_type'] ?: '',
    		$param['relation_id'] ?: '',
    		$param['id'] ?: 0,
    	];
		
		try {
			$db = new MsgModel;
			$result = $db->sendSystemMsg($title,$content,$img,$buessType,$relationId,$id);
			if($result === false){
				return $this->ReError( $db->errorInfo );	
			}
			return $this->ReSucess($result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 删除系统消息
	* /admin/v3/msg/delSystemMsg
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function delSystemMsg($ids){
		try {
			
			$systemMsg = new MsgSystem;
			$result = $systemMsg->where('id','in',$ids)->delete();
			if($result === false){
				return $this->ReError( $systemMsg->errorInfo );	
			}
			
			$msg = MsgModel::where('notice_type',3)->find();
			if( $msg ){
				$msg->unread_num = bcsub($msg->unread_num,count(explode(",",$ids)),0);
				$msg->save();
			}
			
			return $this->ReSucess();	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 获取系统消息
	* /admin/v3/msg/getSystemMsg
	* 需要登录 
	* @is_need_login true
	* @method post
	*/
	public function getSystemMsg(){
		try {
			$msg = MsgSystem::where('notify_type',3)->where('status',0)->select();
			return $this->ReSucess($msg);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	
}



